package com.irs.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.irs.mapper.TbLotDataMapper;
import com.irs.pojo.TbLotData;
import com.irs.pojo.TbLotDataExample;
import com.irs.pojo.TbLotDataExample.Criteria;
import com.irs.pojo.TbLotSearch;
import com.irs.service.DeviceService;
import com.irs.util.MyUtil;
import com.irs.util.ResultUtil;

@Service
public class DeviceServiceImpl implements DeviceService {
	@Autowired
	private TbLotDataMapper tbLotDataMapper;
	
	@Override
	public void addData(TbLotData lotData) {
		tbLotDataMapper.insert(lotData);
	}

	@Override
	public ResultUtil selTbLotData(Integer page, Integer limit, TbLotSearch search) {
		PageHelper.startPage(page, limit);
		TbLotDataExample example=new TbLotDataExample();
		//设置按创建时间降序排序
		example.setOrderByClause("date DESC");
		Criteria criteria = example.createCriteria();
		
		if(search.getKey()!=null&&!"".equals(search.getKey())){
			criteria.andKeyLike("%"+search.getKey()+"%");
		}
		if(search.getCreateTimeStart()!=null&&!"".equals(search.getCreateTimeStart())){
			criteria.andDateGreaterThanOrEqualTo(MyUtil.getDateByString(search.getCreateTimeStart()));
		}
		if(search.getCreateTimeEnd()!=null&&!"".equals(search.getCreateTimeEnd())){
			criteria.andDateLessThanOrEqualTo(MyUtil.getDateByString(search.getCreateTimeEnd()));
		}
		
		List<TbLotData> users = tbLotDataMapper.selectByExample(example);
		PageInfo<TbLotData> pageInfo = new PageInfo<TbLotData>(users);
		ResultUtil resultUtil = new ResultUtil();
		resultUtil.setCode(0);
		resultUtil.setCount(pageInfo.getTotal());
		resultUtil.setData(pageInfo.getList());
		return resultUtil;
	}

}
